﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using cbmobile.BLC;
using cbmobile.DAC;
using PQT.API;
using PQT.API.File;
using PQT.Common;
using PQT.Controls;
using Telerik.WebControls;
using PQT.API.DataDefine.Sys;

namespace cbmobile.MasterUser.UC_Page
{
    public partial class ChiTietGioHang : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["addGioHang"] == null)
                {
                    MessageBox.Show("Giỏ hàng rỗng");
                    Response.Redirect("Index.aspx");
                }
                else
                {
                    DataTable dt = (DataTable)Session["addGioHang"];
                    dtlGioHang.DataSource = dt;
                    dtlGioHang.DataBind();
                    lblTongTien.Text = TinhTien(dt).ToString();
                }

                if (this.dtlGioHang.Items.Count == 0)
                {
                    cmdTiepTuc.Enabled = false;
                }
            }
        }

        private decimal TinhTien(DataTable dt)
        {
            decimal tongtien = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                tongtien += Convert.ToDecimal(dt.Rows[i]["ThanhTien"].ToString());
            }
            return tongtien;
        }

        protected string DropItem(object p_Color, int n)
        {
            string str=Convert.ToString(p_Color);
            string[] strSplit = str.Split(',');
            return strSplit[n];
        }

        protected void cmdXoaHet_Click(object sender, EventArgs e)
        {
            Session.Remove("addGioHang");
            Session.Remove("slsp");
            dtlGioHang.DataSource = null;
            dtlGioHang.DataBind();
            lblTongTien.Text = "0";

        }

        protected void dtlGioHang_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            if (IsPostBack)
            {
                DataListItem dtlItem = e.Item;
                Label labelID = (Label)dtlItem.FindControl("lblMaSP");
                XoaSanPham(labelID.Text);
                TextBox txt = (TextBox)dtlItem.FindControl("txtSoLuong");
                int slsp = Convert.ToInt32(Session["slsp"].ToString()) - Convert.ToInt32(txt.Text);
                Session["slsp"] = slsp;
                if (this.dtlGioHang.Items.Count == 0)
                {
                    this.cmdTiepTuc.Enabled = false;
                }
                Response.Redirect("ChiTietGioHang.aspx");
            }
        }

        protected void cmdMuaThem_Click(object sender, EventArgs e)
        {
            Response.Redirect("Index.aspx");
        }

        protected void dtlGioHang_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            if (IsPostBack)
            {
                DataListItem dtlItem = e.Item;
                Label lbID = (Label)dtlItem.FindControl("lblMaSP");
                TextBox txt = (TextBox)dtlItem.FindControl("txtSoLuong");
                Label lbGia = (Label)dtlItem.FindControl("lblGia");
                int sl = Convert.ToInt32(txt.Text);
                int slc = SL_old(lbID.Text);
                UpdateGH(lbID.Text, sl, Convert.ToDecimal(lbGia.Text));
                int slsp = Convert.ToInt32(Session["slsp"].ToString()) + (Convert.ToInt32(txt.Text) - slc);
                Session["slsp"] = slsp;
                
            }

        }


        private void XoaSanPham(string masp)
        {
            if (Session["addGioHang"] == null)
            {
                MessageBox.Show("Giỏ hàng không có sản phẩm");
            }
            else
            {
                DataTable giohang = (DataTable)Session["addGioHang"];
                int indexofRow = IsExistItemInShoppingCart(masp, giohang);
                if (indexofRow != -1)
                {
                    giohang.Rows.RemoveAt(indexofRow);
                    Session["addGioHang"] = giohang;
                    dtlGioHang.DataSource = giohang;
                    dtlGioHang.DataBind();
                }
            }
        }
       
        private void UpdateGH(string masp, int soluong, decimal gia)
        {
            if (Session["addGioHang"] == null)
            {
                MessageBox.Show("Giỏ hàng không có sản phẩm");
            }
            else
            {
                DataTable giohang = (DataTable)Session["addGioHang"];
                int indexofRow = IsExistItemInShoppingCart(masp, giohang);
                if (indexofRow != -1)
                {
                    giohang.Rows[indexofRow]["SoLuong"] = soluong;
                    giohang.Rows[indexofRow]["ThanhTien"] = soluong * gia;
                    Session["addGioHang"] = giohang;
                    dtlGioHang.DataSource = giohang;
                    dtlGioHang.DataBind();
                    lblTongTien.Text = TinhTien(giohang).ToString();
                }
            }
        }

        private int SL_old(string masp)
        {
            DataTable giohang = (DataTable)Session["addGioHang"];
            int indexofRow = IsExistItemInShoppingCart(masp, giohang);
            int n = 0;
            if (indexofRow != -1)
            {
                n = Convert.ToInt32(giohang.Rows[indexofRow]["SoLuong"]);
            }
            return n;
        }

        private int IsExistItemInShoppingCart(string itemID, DataTable dataTable)
        {
            int indexOfRow = -1;
            for (int dataRow = 0; dataRow < dataTable.Rows.Count; dataRow++)
            {
                if (dataTable.Rows[dataRow]["MaSP"].ToString() == itemID)
                {
                    indexOfRow = dataRow;
                    break;
                }
            }
            return indexOfRow;
        }


        protected void txtSoLuong_TextChanged(object sender, EventArgs e)
        {

            
        }

        private void DatHang()
        {
            BLC_GioHang blcGH = new BLC_GioHang();
            if (Session["User"] != null)
            {
                string makh = Convert.ToString(Session["User"]);
                bool tinhtrang = false;
                DateTime ngaymua = DateTime.Today;

                int phuongthuc = Convert.ToInt32(ddlPhuongThuc.SelectedValue);
                blcGH.Create_DH(Convert.ToInt32(makh.ToString()), tinhtrang, phuongthuc, ngaymua, ngaymua);

                int MaDH = blcGH.Max_MADH();
                for (int i = 0; i < dtlGioHang.Items.Count; i++)
                {
                    Label masp = (Label)dtlGioHang.Items[i].FindControl("lblMaSP");
                    TextBox sl = (TextBox)dtlGioHang.Items[i].FindControl("txtSoLuong");
                    Label gia = (Label)dtlGioHang.Items[i].FindControl("lblGia");
                    Label tensp = (Label)dtlGioHang.Items[i].FindControl("lblTenSP");
                    DropDownList mau = (DropDownList)dtlGioHang.Items[i].FindControl("dropMau");
                    Label loaisp = (Label)dtlGioHang.Items[i].FindControl("lblLoaiSP");
                    switch (loaisp.Text)
                    {
                        case "DT":
                            blcGH.Create_CTDH_DT(MaDH, Convert.ToInt32(masp.Text), Convert.ToInt32(sl.Text), Convert.ToDecimal(gia.Text), tensp.Text, mau.Text);
                            break;
                        case "PK":
                            blcGH.Create_CTDH_PK(MaDH, Convert.ToInt32(masp.Text), Convert.ToInt32(sl.Text), Convert.ToDecimal(gia.Text), tensp.Text, mau.Text);
                            break;
                        case "LK":
                            blcGH.Create_CTDH_LK(MaDH, Convert.ToInt32(masp.Text), Convert.ToInt32(sl.Text), Convert.ToDecimal(gia.Text), tensp.Text, mau.Text);
                            break;

                    }
                }
                Session.Remove("addGioHang");
                Session.Remove("slsp");
                Response.Redirect("Tks.aspx");
            }
            else
                if (txtDiaChi.Text == "")
                {
                    MessageBox.Show("Bạn chưa đăng nhập hoặc điền thông tin liên hệ.");
                }
                else
                {
                    //tao thanh vien
                    BLC_Member blcM = new BLC_Member();
                    int n = ddlGioiTinh.SelectedIndex;
                    bool gioitinh;
                    if (n == 0)
                        gioitinh = true;
                    else
                        gioitinh = false;
                    blcM.Insert(null, txtHo.Text + " " + txtTen.Text, DateTime.Today, gioitinh, txtEmail.Text, null, txtDiaChi.Text, txtSDT.Text, 3);

                    //them don hang
                    string makh = Convert.ToString(Session["User"]);
                    bool tinhtrang = false;
                    DateTime ngaymua = DateTime.Today;

                    int phuongthuc = Convert.ToInt32(ddlPhuongThuc.SelectedValue);
                    blcGH.Create_DH(blcM.Last_Member(), tinhtrang, phuongthuc, ngaymua, ngaymua);

                    int MaDH = blcGH.Max_MADH();
                    for (int i = 0; i < dtlGioHang.Items.Count; i++)
                    {
                        Label masp = (Label)dtlGioHang.Items[i].FindControl("lblMaSP");
                        TextBox sl = (TextBox)dtlGioHang.Items[i].FindControl("txtSoLuong");
                        Label gia = (Label)dtlGioHang.Items[i].FindControl("lblGia");
                        Label tensp = (Label)dtlGioHang.Items[i].FindControl("lblTenSP");
                        DropDownList mau = (DropDownList)dtlGioHang.Items[i].FindControl("dropMau");
                        Label loaisp = (Label)dtlGioHang.Items[i].FindControl("lblLoaiSP");
                        switch (loaisp.Text)
                        {
                            case "DT":
                                blcGH.Create_CTDH_DT(MaDH, Convert.ToInt32(masp.Text), Convert.ToInt32(sl.Text), Convert.ToDecimal(gia.Text), tensp.Text, mau.SelectedValue);
                                break;
                            case "PK":
                                blcGH.Create_CTDH_PK(MaDH, Convert.ToInt32(masp.Text), Convert.ToInt32(sl.Text), Convert.ToDecimal(gia.Text), tensp.Text,mau.SelectedValue);
                                break;
                            case "LK":
                                blcGH.Create_CTDH_LK(MaDH, Convert.ToInt32(masp.Text), Convert.ToInt32(sl.Text), Convert.ToDecimal(gia.Text), tensp.Text, mau.SelectedValue);
                                break;

                        }
                    }
                    Session.Remove("addGioHang");
                    Session.Remove("slsp");
                    Response.Redirect("Tks.aspx");
                }
        }

        protected void cmdTiepTuc_Click(object sender, EventArgs e)
        {
            DatHang();
        }
    }
}